﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Hutech.CRM.Library.Views;
using Hutech.CRM.Library.Models.Entities;
using nvn.Library.Patterns;
using nvn.Library.Utility;

namespace Hutech.CRM.Web.Controls.QuanLyDanhMucThongTin
{
    public partial class ListLoaiDanhMucControl : System.Web.UI.UserControl, IListLoaiDanhMucView
    {
        #region Cac su kien uy thac
        private DanhMucThongTin _objecOnDelaga;
        public DanhMucThongTin ObjecOnDelaga
        {
            get { return _objecOnDelaga; }
            set { _objecOnDelaga = value; }
        }
        public event EventHandler<OneParameterEventAgrs<string>> ReloadData;
        protected void onReloadData(string paramater)
        {
            if (ReloadData != null)
            {
                ReloadData(this, new OneParameterEventAgrs<string>(paramater));
            }
        }
        #endregion
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
                OnFristLoadEventHandler();
        }
        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);
            this.InitView();
            this.CheckBoxIsActive.CheckedChanged += new EventHandler(CheckBoxIsActive_CheckedChanged);
            this.DataListLoaiDanhMuc.ItemCommand += new DataListCommandEventHandler(DataListLoaiDanhMuc_ItemCommand);
            this.ImageButtonThemMoi.Click += new EventHandler(ImageButtonThemMoi_Click);
        }

        
        

        #region event
        void DataListLoaiDanhMuc_ItemCommand(object source, DataListCommandEventArgs e)
        {
            DanhMucThongTin item = new DanhMucThongTin();
            item.DanhMucGuid = Guid.Parse(DataListLoaiDanhMuc.DataKeys[e.Item.ItemIndex].ToString());
            if (e.CommandName.Equals("ApplyCommand"))
            {
                item.DanhMucName = ((TextBox)e.Item.FindControl("TextBoxItemName")).Text;
                item.DanhMucID = ((TextBox)e.Item.FindControl("TextBoxItemID")).Text;
                item.LoaiDanhMucGuid = Guid.Empty;
                item.LoaiDanhMucName = "Root";
                item.IsActive = ((CheckBox)e.Item.FindControl("CheckBoxIsActive")).Checked;
            }
            if (e.CommandName.Equals("DetailCommand"))
            {
                item.DanhMucName = ((HiddenField)e.Item.FindControl("HiddenFieldLoaiDanhMucName")).Value;
                _objecOnDelaga = item;
                this.onReloadData("DetailCommand");
                //return;
            }
            this.OnListLoaiDanhMuc_ItemCommand(e.CommandName, DataListLoaiDanhMuc.DataKeys[e.Item.ItemIndex].ToString(),
                e.Item.ItemIndex,
                item);
        }
        void ImageButtonThemMoi_Click(object sender, EventArgs e)
        {
            this.onReloadData("AddNew");

        }
        void CheckBoxIsActive_CheckedChanged(object sender, EventArgs e)
        {
            this.OnIsActiveChecked();
        }
        #endregion

        #region Implement from View class
        #region properties
        public List<DanhMucThongTin> AllItemCategories
        {
            set
            {
                DataListLoaiDanhMuc.DataSource = value;
                DataListLoaiDanhMuc.DataBind();
            }
        }

        public bool ActiveItem
        {
            get
            {
                return Convert.ToBoolean(CheckBoxIsActive.Checked);
            }
        }

        public int EditItemIndex
        {
            set { DataListLoaiDanhMuc.EditItemIndex = value; }
        }

        public string CountLine
        {
            set
            {
                if (!string.IsNullOrEmpty(value))
                    LblSoLuong.Text = value;
                else
                    LblSoLuong.Text = "0";
            }
        }

        public string ErrorMessage
        {
            set
            {
                if (value != null)
                    ScriptManager.RegisterStartupScript(this.Page, this.GetType(), "Show", ShowError.Show(value), false);
            }
        }
        #endregion

        #region event

        public event EventHandler<EventArgs> IsActive_Checked;
        private void OnIsActiveChecked()
        {
            if (IsActive_Checked != null)
                IsActive_Checked(this, new EventArgs());
        }
        public event EventHandler<EventArgs> AddNew_EvenAgrs;
        private void OnAddnew()
        {
            if (AddNew_EvenAgrs != null)
                AddNew_EvenAgrs(this, new EventArgs());
        }

        public event EventHandler<ItemCommandArgs<DanhMucThongTin>> ListLoaiDanhMuc_ItemCommand;
        private void OnListLoaiDanhMuc_ItemCommand(string parameterName, string parameterValue, int itemIndex, DanhMucThongTin myType)
        {
            if (ListLoaiDanhMuc_ItemCommand != null)
                ListLoaiDanhMuc_ItemCommand(this, new ItemCommandArgs<DanhMucThongTin>(parameterName, parameterValue, itemIndex, myType));
        }

        public event EventHandler<ItemDataBoundArgs> ListLoaiDanhMuc_ItemDataBound;
        private void OnListLoaiDanhMuc_ItemDataBound(string parameterName)
        {
            if (ListLoaiDanhMuc_ItemDataBound != null)
                ListLoaiDanhMuc_ItemDataBound(this, new ItemDataBoundArgs(parameterName));
        }

        public event EventHandler<EventArgs> FristLoadEventHandler;
        private void OnFristLoadEventHandler()
        {
            if (FristLoadEventHandler != null)
                FristLoadEventHandler(this, new EventArgs());
        }
        public event EventHandler<OneParameterEventAgrs<DanhMucThongTin>> Reload_EvetAgrs;
        protected void onreloaddata(DanhMucThongTin objectOnDelega)
        {
            if (Reload_EvetAgrs != null)
            {
                Reload_EvetAgrs(this, new OneParameterEventAgrs<DanhMucThongTin>(objectOnDelega));
            }
        }
        #endregion
        #endregion


        internal void Show()
        {
            this.OnFristLoadEventHandler();
        }
    }
}